/*
 * BanBu_EmoStoreViewController
 * @description 本文件提供对话中的表情商店的viewController
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供对话中的表情商店的viewController
 */


#import "BanBu_EmoStoreViewController.h"
#import "BanBu_EmoStoreCell.h"
#import "BanBu_SmileViewAPI.h"
#import "BanBu_NavButton.h"
#import "BanBu_EmoManger.h"
#import "BanBu_DetailEmoViewController.h"
#import "UIImageView+WebCache.h"
#import "AppCommunicationManager.h"
#import "AppDataManager.h"
#import "BanBu_ImageView.h"

@interface BanBu_EmoStoreViewController ()

@end

@implementation BanBu_EmoStoreViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = NSLocalizedString(@"emoStore", nil);
    [_emoTable setFrame:self.view.bounds];
    MyAppDataManager.emoStoreController = self;
    
    self.emoTable.backgroundColor = [UIColor clearColor];
    BanBu_ImageView *img = [[[BanBu_ImageView alloc] initWithBackGroundImageType:UIImageViewCreatTypeTable] autorelease];
    self.view.backgroundColor = [UIColor colorWithPatternImage:img.image];
    self.emoTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    _recommendBgView.backgroundColor = [UIColor darkGrayColor];
    
    _emoTable.tableHeaderView = _recommendBgView;
    
    [self createEmoMangerButton];
    //先判断缓存数据是否过期
    BOOL flag = [BanBu_CommUtils needUpdateCache:EmoShopTimeKey timeInterval:EmoShopTimeInterval];
    //已经过期
    if (flag)
    {
        //获取表情列表
        [AppComManager getBanBuData:BanBu_GetFaceStoreList par:nil delegate:self];
    }
    else
    {
        //获取数据源,修改2014/07/16
        NSDictionary * EmoShopAmsgDic = [BanBu_CommUtils getLocalData:EmoShopDataKey];
        [self refreshView:EmoShopAmsgDic];
    }
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [_emoTable reloadData];
}

- (void)banbuRequestDidFinished:(NSDictionary *)resDic error:(NSError *)error
{
    if ([AppComManager respondsDic:resDic isFunctionData:BanBu_GetFaceStoreList])//表情商店
    {
        [BanBu_CommUtils setLocalData:resDic key:EmoShopDataKey];
        [BanBu_CommUtils setCacheTime:EmoShopTimeKey];
        [self refreshView:resDic];
    }
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    MyAppDataManager.emoStoreController = nil;
    [_recommendBgView release];
    [_emoTable release];
    [_emoArr release],_emoArr = nil;
    [super dealloc];
}
- (void)viewDidUnload
{
    [self setRecommendBgView:nil];
    [self setEmoTable:nil];
    [super viewDidUnload];
}

-(void)createEmoMangerButton
{
    //添加“表情管理”按钮
    BanBu_NavButton * emoMangerButton = [[BanBu_NavButton alloc] initWithButtonType:UIButtonCreatTypeNormal];
    emoMangerButton.position = ButtonPositionRight;
    emoMangerButton.frame=CGRectMake(0, 0,20, 17);
    emoMangerButton.tag = 101;
    [emoMangerButton addTarget:self action:@selector(goinEmoManger) forControlEvents:UIControlEventTouchUpInside];
    [emoMangerButton setImage:[UIImage imageNamed:@"btn_emoset.png"] forState:UIControlStateNormal];
    UIBarButtonItem *delItem = [[[UIBarButtonItem alloc] initWithCustomView:emoMangerButton] autorelease];
    self.navigationItem.rightBarButtonItem = delItem;
    
    
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 40)];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:18];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    CGFloat btnLen = [NSLocalizedString(@"emoStore", nil) sizeWithFont:[UIFont systemFontOfSize:18] constrainedToSize:CGSizeMake(100, 30)].width;
    label.text = NSLocalizedString(@"emoStore", nil);
    label.frame = CGRectMake(0, 0, btnLen+20, 40);
    
    [self.navigationItem.titleView.superview bringSubviewToFront:self.navigationItem.titleView];
    self.navigationItem.titleView = label;
    
    [label release];
}

//表情商店：表情管理
-(void)goinEmoManger
{
    BanBu_EmoManger * emoManger = [[BanBu_EmoManger alloc]init];
    [self.navigationController pushViewController:emoManger animated:YES];
    [emoManger release];
}

//刷新界面
-(void)refreshView:(NSDictionary *)emoInfoDic
{
    if ([emoInfoDic valueForKey:@"list"])
    {
        self.emoArr = [[NSMutableArray alloc]initWithArray:[emoInfoDic valueForKey:@"list"]];
        
        [_emoTable reloadData];
        [self gifView:[_emoArr objectAtIndex:0]];
    }
    //2014年10月23日10:01:08 by yinlinlin 之前获取数据错误，重新请求数据
    if (self.emoArr.count == 0)
    {
        //获取表情列表
        [AppComManager getBanBuData:BanBu_GetFaceStoreList par:nil delegate:self];
    }
}

-(void)gifView:(NSDictionary *)emoInfoDic
{
    NSString * gifPath = [NSString stringWithFormat:@"%@banner.gif",[emoInfoDic valueForKey:KeyPath]];
    [_recommendBgView setImageWithUrlString:gifPath withFolderName:[AppComManager remove_:[emoInfoDic valueForKeyPath:Keyid]] withImageName:@"banner.gif"];
}

#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _emoArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifier = @"cellIdentifier";
    BanBu_EmoStoreCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell==nil)
    {
        NSArray * objects =[[NSBundle mainBundle]loadNibNamed:@"BanBu_EmoStoreCell" owner:self options:nil];
        
        for (id object in objects)
        {
            if ([object isMemberOfClass:[BanBu_EmoStoreCell class]])
            {
                cell = object;
                cell.backgroundColor = [UIColor clearColor];
                break;
            }
        }
        
        [[NSNotificationCenter defaultCenter]addObserver:cell selector:@selector(reloadView) name:KFinishDownloadingEmo object:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }
    
    NSDictionary * emoInfo = [_emoArr objectAtIndex:indexPath.row];
    
    if ([[emoInfo valueForKey:KeyDownloadState] boolValue])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    //设置表情详情
    [cell setEmoInfo:emoInfo];
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BanBu_DetailEmoViewController * detailEmoController = [[BanBu_DetailEmoViewController alloc]init];
    
    detailEmoController.emoInfoDic = [_emoArr objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:detailEmoController animated:YES];
    [detailEmoController release];
}
@end
